/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetAdminSavedSearches
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public boolean domainAuthSufficient(Map context)
/*     */   {
/*  40 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException {
/*  44 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  45 */     Account acct = getRequestedAccount(zsc);
/*     */     
/*  47 */     checkAccountRight(zsc, acct, Rights.Admin.R_viewAdminSavedSearch);
/*     */     
/*  49 */     Element response = zsc.createElement(AdminConstants.GET_ADMIN_SAVED_SEARCHES_RESPONSE);
/*     */     
/*  51 */     HashSet<String> specificSearches = null;
/*  52 */     for (Iterator it = request.elementIterator("search"); it.hasNext();) {
/*  53 */       if (specificSearches == null)
/*  54 */         specificSearches = new HashSet();
/*  55 */       Element e = (Element)it.next();
/*  56 */       String name = e.getAttribute("name");
/*  57 */       if (name != null) {
/*  58 */         specificSearches.add(name);
/*     */       }
/*     */     }
/*  61 */     handle(acct, response, specificSearches);
/*  62 */     return response;
/*     */   }
/*     */   
/*     */   public void handle(Account acct, Element response, HashSet<String> specificSearches) throws ServiceException {
/*  66 */     String[] searches = acct.getMultiAttr("zimbraAdminSavedSearches");
/*     */     
/*  68 */     for (int i = 0; i < searches.length; i++) {
/*  69 */       String search = searches[i];
/*  70 */       AdminSearch as = AdminSearch.parse(search);
/*     */       
/*  72 */       if ((specificSearches == null) || (specificSearches.contains(as.getName()))) {
/*  73 */         response.addElement("search").addAttribute("name", as.getName()).setText(as.getQuery());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes) {
/*  79 */     relatedRights.add(Rights.Admin.R_viewAdminSavedSearch);
/*     */   }
/*     */   
/*     */   public static class AdminSearch
/*     */   {
/*     */     private static final String DELIM = " : ";
/*  85 */     private static final int DELIM_LEN = " : ".length();
/*     */     
/*     */ 
/*  88 */     String mName = "";
/*  89 */     String mQuery = "";
/*     */     
/*     */     public AdminSearch(String name, String query) {
/*  92 */       if (name != null) {
/*  93 */         this.mName = name;
/*     */       }
/*  95 */       if (query != null)
/*  96 */         this.mQuery = query;
/*     */     }
/*     */     
/*  99 */     public String getName() { return this.mName; }
/* 100 */     public String getQuery() { return this.mQuery; }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     public static AdminSearch parse(String search)
/*     */     {
/* 107 */       String name = null;
/* 108 */       String query = null;
/* 109 */       int delimAt = search.indexOf(" : ");
/* 110 */       if (delimAt == -1)
/*     */       {
/* 112 */         query = search;
/*     */       } else {
/* 114 */         name = search.substring(0, delimAt);
/* 115 */         if (search.length() > delimAt + DELIM_LEN) {
/* 116 */           query = search.substring(delimAt + DELIM_LEN);
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 121 */       if (name != null) {
/* 122 */         name = name.replaceAll("\\\\:", ":");
/*     */       }
/* 124 */       return new AdminSearch(name, query);
/*     */     }
/*     */     
/*     */     public String encode() {
/* 128 */       return this.mName.replaceAll(":", "\\\\:") + " : " + this.mQuery;
/*     */     }
/*     */   }
/*     */   
/*     */   private static void test(String search) {
/* 133 */     AdminSearch as = AdminSearch.parse(search);
/*     */     
/* 135 */     System.out.println("[" + search + "] => [" + as.getName() + "] [" + as.getQuery() + "]");
/* 136 */     String encoded = as.encode();
/* 137 */     assert (search.equals(encoded));
/*     */   }
/*     */   
/*     */   private static void test(AdminSearch search) {
/* 141 */     String encoded = search.encode();
/* 142 */     System.out.println("[" + search.getName() + "] [" + search.getQuery() + "] => [" + encoded + "]");
/*     */     
/* 144 */     AdminSearch parsed = AdminSearch.parse(encoded);
/* 145 */     String encoded2 = parsed.encode();
/* 146 */     assert (encoded.equals(encoded2));
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 150 */     test("name : query");
/* 151 */     test("n a m e : q u e r y");
/* 152 */     test("name\\:\\ : que : ry");
/* 153 */     test("name \\: : query");
/* 154 */     test("\\: : query");
/*     */     
/* 156 */     test(new AdminSearch("name :::", ":::query"));
/* 157 */     test(new AdminSearch("\\:", " \\:::query"));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetAdminSavedSearches.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */